package com.hnkywl.sts.dao.cwgl;

import com.hnkywl.sts.entity.cggl.Jsd;
import com.hnkywl.sts.entity.zjgl.Pj;
import com.hnkywl.sts.entity.zjgl.Pjdr;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import oracle.jdbc.OracleCallableStatement;
import org.hibernate.SQLQuery;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.*;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-7-7
 * Time: 上午11:12
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class FpdrDao extends SimpleHibernateTemplate  {

    /**
     * 获得凭证主信息
     * @return
     */
    public Map findVoucherMain(){
        String sql = "select '记'  as \"vchrtypecode\", yg.xm   as \"createuser\", sysdate as \"createdate\" " +
                " from  t_sys_yg yg where yg.deleted = 0  and yg.id = ? ";
        SQLQuery sqlQuery = getSession().createSQLQuery(sql);
        sqlQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        sqlQuery.setLong(0, YgService.getCurrentYg().getId());
        return (Map) sqlQuery.uniqueResult();
    }
    /**
     * @param type  类型
     * @param jsdId 结算单ID
     * @return
     */
    public List<Map> getVoucherItems(Integer type, Long jsdId,Jsd jsd) {
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try {
            con = getSession().connection();
            if(jsd.getGlqy().className()=="Nbgs"){
                call = con.prepareCall("{Call VOUCHER_NBFP(?,?,?)}");
            }else if(jsd.getGlqy().className()=="Qyjg"){
                call = con.prepareCall("{Call VOUCHER_FP(?,?,?)}");
            }
            call.setInt(1, type);
            call.setLong(2, jsdId);
            call.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            //====================================
            rs = ((OracleCallableStatement) call).getCursor(3);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            List<Map> result = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                result.add(r);
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }

    private void closeResource(ResultSet rs, Statement s, Connection con) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (s != null)
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (con != null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
    }
}
